﻿using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;

namespace Ags.MonsterTools.PingTool
{
    internal class Pinger
    {
        internal static async Task ContinuousPingAsync(IPAddress address, int timeout, int delayBetweenPings, IProgress<PingResult> progress, CancellationToken cancellationToken)
        {
            var ping = new Ping();
            while (!cancellationToken.IsCancellationRequested)
            {
                var timestamp = DateTime.Now;
                var result = await ping.SendPingAsync(address, timeout).ConfigureAwait(false);
                var pingResult = new PingResult()
                {
                    Result = result, 
                    Time = timestamp,
                    Timeout = timeout,
                    Interval = delayBetweenPings,
                    PingTarget = address
                };
                progress.Report(pingResult);
                await Task.Delay(delayBetweenPings).ConfigureAwait(false);
            }
        }
    }
}
